<template>
  <div>
    <div>
      <p style="font-size: 26px;color: #8c939d">请输入你要查询的商品名称</p>
      <el-form style="text-align: left" :model="searchMsg" status-icon :inline="true" ref="searchFrom"
               label-width="100px" class="demo-form-inline">
        <el-form-item label="商品名称" prop="gname">
          <el-input v-model="searchMsg.gname"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search()">搜索</el-button>
          <el-button type="primary" @click="restSearch()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div v-if="discountMsg===null">
      <el-empty description="暂无数据"></el-empty>
    </div>
    <div v-else>
      <el-table
          :data="discountMsg"
          border
          style="width: 100%">
        <el-table-column
            fixed
            label="预览图"
            width="160">
          <template slot-scope="scope">
            <img :src="scope.row.gimg" width="120px" height="120px">
          </template>
        </el-table-column>
        <el-table-column
            prop="gname"
            label="商品名称"
        >
          <template slot-scope="scope">
            {{ scope.row.gname }}
          </template>
        </el-table-column>
        <el-table-column
            prop="gnub"
            label="商品库存"
        >
          <template slot-scope="scope">
            {{ scope.row.gnub }}
          </template>
        </el-table-column>
        <el-table-column
            prop="gprice"
            label="商品原价"
        >
          <template slot-scope="scope">
            <el-tag>￥{{ scope.row.gprice }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="折扣后价格"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.serDiscount===null">暂无折扣</el-tag>
            <el-tag type="danger" v-else-if="Date.parse(new Date().toDateString())>=Date.parse(scope.row.serDiscount.gdend)">活动已过期</el-tag>
            <el-tag type="success" v-else>￥{{ scope.row.gprice - scope.row.serDiscount.discount }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="serDiscount.gdstart"
            label="开始日期"
        >

        </el-table-column>
        <el-table-column
            prop="serDiscount.gdend"
            label="结束日期"
        >
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
        >
          <template slot-scope="scope">
            <el-button @click="popModify(scope.row)" type="primary">修改</el-button>
            <el-button v-if="scope.row.serDiscount!=null" @click="remove(scope.row)" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[3,5,10,20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!--    修改-->
    <el-dialog
        title="修改"
        :visible.sync="modifyFlag"
        width="20%"
        :before-close="handleClose">
      <el-form :model="modifyMsg" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item>
          <img :src="modifyMsg.gimg ">
        </el-form-item>
        <el-form-item label="货物名称" prop="gname">
          {{ modifyMsg.gname }}
        </el-form-item>
        <el-form-item label="货物价格" prop="gprice">
          ￥{{ modifyMsg.gprice }}
        </el-form-item>
        <el-form-item label="折扣价格" prop="discount">
          <el-input v-model="modifyMsg.discount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="折扣描述" prop="dicmsg">
          <el-input
              type="textarea"
              :rows="2"
              placeholder="输入折扣信息"
              v-model="modifyMsg.dicmsg">
          </el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="gdstart">
          <el-date-picker
              v-model="modifyMsg.gdstart"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择折扣开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="gdend">
          <el-date-picker
              v-model="modifyMsg.gdend"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetime"
              placeholder="选择折扣结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="modify()">提交</el-button>
          <el-button @click="modifyFlag = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>

import axios from "axios";

export default {
  name: "rentmenu",
  data() {
    return {
      // 展示的商品信息
      discountMsg: [],
      // 搜索的条件
      searchMsg: {
        gname: ""
      },
      // 分页数据
      pageIndex: 1,
      pageSize: 5,
      total: 0,
      // 修改弹窗
      modifyFlag: false,
      // 修改数据
      modifyMsg: {
        gname:"",
        gid:"",
        gimg:"",
        gprice:"",
        dicmsg:"",
        discount:"",
        gdend:"",
        gdstart:"",
      }
    };
  },
  // 页面初始化方法
  created() {
    this.flushPage()
  },
  methods: {
    // 分页的方法，当页面大小改变
    handleSizeChange(val) {
      this.pageSize = val
      this.flushPage()
    },
    // 当页面下标改变
    handleCurrentChange(val) {
      this.pageIndex = val
      this.flushPage()
      const a = [1, 2, 3];
      a.forEach(e=>{
        console.log(e)
      })
    },
    // 弹出修改框
    popModify(rowMsg) {

      this.modifyMsg.gname=rowMsg.gname
      this.modifyMsg.gid=rowMsg.gid
      this.modifyMsg.gimg=rowMsg.gimg
      this.modifyMsg.gprice=rowMsg.gprice
      if (rowMsg.serDiscount != null){
        this.modifyMsg.dicmsg=rowMsg.serDiscount.dicmsg
        this.modifyMsg.discount=rowMsg.serDiscount.discount
        this.modifyMsg.gdend=rowMsg.serDiscount.gdend
        this.modifyMsg.gdstart=rowMsg.serDiscount.gdstart
        this.modifyMsg.gdid=rowMsg.serDiscount.gdid

      }else {
        this.modifyMsg.dicmsg=""
        this.modifyMsg.discount=""
        this.modifyMsg.gdend=""
        this.modifyMsg.gdstart=""
        this.modifyMsg.gdid=""
      }
      console.log()
      console.log()
      this.modifyFlag = true
    },
    // 对话框关闭之后
    handleClose() {
      this.$nextTick(() => {
        this.$refs['ruleForm'].resetFields();
      })
      this.modifyFlag = false
    },
    resetForm() {
      this.$nextTick(() => {
        this.$refs['ruleForm'].resetFields();
      })
    },
    // 删除这个
    remove(rowMsg) {
      console.log(rowMsg.serDiscount.gdid)
      this.$http.get("/Discount/Remove/"+rowMsg.serDiscount.gdid).then(resp=>{
        if (resp.data.code === 2000) {
          this.$notify({
            title: "消息",
            message: "删除成功",
            duration: 4000
          })
          this.flushPage()
        } else {
          this.$notify.error({
            title: '错误',
            message: '删除失败！' + resp.data.msg
          });
          this.flushPage()
        }

      })
    },
    // 修改折扣
    modify() {
      console.log(this.modifyMsg)
      axios.post("/Discount/Modify", this.modifyMsg).then(resp => {
        if (resp.data.code === 2000) {
          this.$notify({
            title: "消息",
            message: "更新成功",
            duration: 4000
          })
          this.flushPage()
        } else {
          this.$notify.error({
            title: '错误',
            message: '更新失败！' + resp.data.msg
          });
        }
        this.flushPage()
      })
      this.modifyFlag = false

    },
    // 执行搜索方法
    search() {
      if (this.searchMsg !== '' && this.searchMsg != null) {
        this.$http.post("/Discount/GetDiscounts", this.searchMsg).then(resp => {
          console.log(resp.data.data)
          this.discountMsg = resp.data.data
        })
      }
    },
    // 重置表单
    restSearch() {
      this.$nextTick(() => {
        this.$refs['searchFrom'].resetFields();
      })
    },
    // 刷新页面的方法
    flushPage() {
      axios.get("/Discount/List/" + this.pageIndex + "/" + this.pageSize).then(resp => {
        this.total = resp.data.data.total
        this.discountMsg = resp.data.data.records;
        console.log(this.discountMsg)
      })
    },
  }
}

</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
